
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate, isLogisticsSupervisor = false;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

//判断当前用户是否包含物流主管角色 角色 true:是 false 否
checkLogisticsSupervisor();

function checkLogisticsSupervisor() {
    $.ajax({
        url: 'customer/isLogisticsSupervisor',
        async: false,
        success: function (data) {
            if (data.status == 200) {
                isLogisticsSupervisor = data.data;
            }
        }
    });
}

layer.ready(function () {
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $('#operationNo').append(new Option(data.data[i].name, data.data[i].code));
                }
                form.render('select');
            }
        }
    });
});

layui.use(['dropdown'], function () {
    let dropdown = layui.dropdown;
    dropdown.onFilter("initStatistics", function (event) {
        switch (event) {
            case "initStatisticsInfo":
                parent.intab("views/jsp/WarehouseManagement/tpl/initStatisticsInfo.jsp","初始化统计")
                break
        }
    });
})


form.render();//表单刷新
table.render({
    elem: '#initializetheselect',
    url: 'initStock/getListByPage'
    ,toolbar: '#initializethetoolbar'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field:'billNo', title:'入库单号',sort:true,event: "billNo"}
        ,{field:'createDate', title:'创建日期',sort:true}
        ,{field:'operator', title:'操作人',sort:true}
        ,{field:'inStorePerson', title:'入库人',sort:true}
        ,{field:'warehouseName', title:'仓库名称',sort:true}
        ,{field:'memo', title:'备注'}
        ,{fixed: 'right', width:240, toolbar: '#initializetheeditor', title:'操作'}
    ]]
    ,id: 'initializethetableid'
    ,height: $(document).height() - $('#initializetheselect').offset().top - 50
    ,page: true,
    parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    },
});
table.on('tool(initializethetable)', function(obj){
    let data = obj.data;
    let id = data.id;
    if(obj.event === 'update') {
        parent.intab("views/jsp/WarehouseManagement/tpl/initUpdate.jsp?id="+obj.data.id,"修改初始化")
    }else if(obj.event === 'delect'){
        layer.confirm("确认删除？",
            confirmInfo(3,6),
            function (index) {
                layer.close(index);
                let dataBases={
                    id:id,
                    status:data.status
                }
                $.ajax({
                    type:"POST",
                    dataType:"json",
                    contentType: 'application/json',
                    data:JSON.stringify(dataBases),
                    url:"initStock/delete",
                    success:function(res){
                        if(res.status == 200){
                            layer.msg("删除成功", {icon: 6});
                            table.reload('initializethetableid', {
                                page: {
                                    limit:10,
                                    curr: 1 //重新从第 1 页开始
                                }
                                ,where: data.field
                            }, 'data');
                            return false;
                        }else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
    }else if(obj.event === 'submits'){
        layer.confirm('确定提交审批吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 1, "提交审批");
        });
    }else if(obj.event === 'through'){
        layer.confirm('确定审批通过吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 2, "审批通过");
        });
    }else if(obj.event === 'backtothe'){
        layer.confirm('确定审批退回吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 3, "审批退回");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定完成吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 4, "完成");
        });
    }else if(obj.event === 'reduction'){
        layer.confirm('确定还原吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 5, "还原");
        });
    }else if (obj.event === "billNo"){
        parent.intab("views/jsp/WarehouseManagement/tpl/viewinitializethe.jsp?id="+obj.data.id,"初始化明细")
    }
});

function operationData(data, operation, msg){
    let fields = {
        id : data.id,
        billNo : data.billNo,
        operation: operation
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(fields),
        url:"initStock/operation",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('initializethetableid', {
                    page: {
                        limit:10,
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

form.on('submit(submitform)', function(data){
    table.reload('initializethetableid', {
        page: {
            limit:10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

table.on('toolbar(initializethetable)', function(obj){
    switch(obj.event){
        case 'news':
            parent.intab("views/jsp/WarehouseManagement/tpl/newupinitializethe.jsp","新增初始化")
            break;
    };
});


$("#statistical").on("click",function () {
    parent.intab("views/jsp/WarehouseManagement/tpl/statistics.jsp","初始化统计")
})

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}